package dataStructDui;

import java.util.Comparator;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.Queue;

public class test {
    public static void main(String[] args) {
        Student student1 = new Student("zhangsan",10);
        Student student2 = new Student("zhangsan",20);

        NameComparator nameComparator = new NameComparator();
        int ret = nameComparator.compare(student1,student2);
        System.out.println(ret);

        AgeComparator ageComparator = new AgeComparator();
        int ret2 = ageComparator.compare(student1,student2);
        System.out.println(ret2);
    }
}

class NameComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1,Student o2) {
        return o1.name.compareTo(o2.name);
    }
}

class AgeComparator implements Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age - o2.age;
    }
}

 class Student implements Comparable<Student>{
    public int age;
    public String name;

     public Student(String name,int age) {
         this.age = age;
         this.name = name;
     }

     @Override
     public int compareTo(Student o) {
         return this.age - o.age;
     }

     @Override
     public boolean equals(Object o) {
         if (this == o) return true;
         if (o == null || getClass() != o.getClass()) return false;
         Student student = (Student) o;
         return age == student.age && Objects.equals(name, student.name);
     }

     @Override
     public int hashCode() {
         return Objects.hash(age, name);
     }


     public int[] smallestK(int[] arr, int k) {
         int[] ret = new int[k];
         if(arr == null || k == 0){
             return ret;
         }
         Queue<Integer> minHeap = new PriorityQueue<>(k);
         // 1，遍历数组的前k个 放到堆中
         for (int i = 0; i < k; i++) {
             minHeap.offer(ret[i]);
         }
         // 2，遍历剩下的k-1个，每次都和堆顶元素比较
         //   如果堆顶元素大，就出堆
         for (int i = k; i < arr.length; i++) {
             int val = minHeap.peek();
             if(val > arr[i]){
                 minHeap.poll();
                 minHeap.offer(arr[i]);
             }
         }
         // 返回这k个数
         for (int i = 0; i < k; i++) {
             ret[i] = minHeap.poll();
         }
         return ret;
     }

 }